package com.ruoyi.project.module.group.domain;

import com.google.common.collect.Lists;
import com.ruoyi.project.module.groupProperty.domain.GroupProperty;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * com.ruoyi.project.module.group.domain.GroupPropertyTree
 * <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  19-4-16 上午10:40<br>
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/
public class GroupPropertyTree extends GroupProperty {

    //节点对应的应用属性配置信息,会存在多个
    private List<ApplicationPropertyTree> apTreeList;

    /**
     * 存放root下所有节点
     */
    private List<GroupPropertyTree> nodeList;

    /**
     * 存放父级
     */
    private GroupPropertyTree parentTree;

    private String fullNameOfProperty;

    private List<GroupPropertyTree> childrenList;

    public List<GroupPropertyTree> getChildrenList() {
        return childrenList;
    }

    public void setChildrenList(List<GroupPropertyTree> childrenList) {
        this.childrenList = childrenList;
    }

    public void addChildren(List<GroupPropertyTree> childrenList) {
        if(this.childrenList==null){
            this.childrenList= Lists.newArrayList();
        }
        this.childrenList.addAll(childrenList);
    }

    public String getFullNameOfProperty() {
        return fullNameOfProperty;
    }

    public void setFullNameOfProperty(String fullNameOfProperty) {
        this.fullNameOfProperty = fullNameOfProperty;
    }

    public List<GroupPropertyTree> getNodeList() {
        return nodeList;
    }

    public void setNodeList(List<GroupPropertyTree> nodeList) {
        this.nodeList = nodeList;
    }

    public void addNode(GroupPropertyTree... node) {
        if(this.nodeList==null){
            this.nodeList=Lists.newArrayList();
        }
        this.nodeList.addAll(Lists.newArrayList(node));
    }



    public GroupPropertyTree getParentTree() {
        return parentTree;
    }

    public void setParentTree(GroupPropertyTree parentTree) {
        this.parentTree = parentTree;
    }

    public List<ApplicationPropertyTree> getApTreeList() {
        return apTreeList;
    }

    public void setApTreeList(List<ApplicationPropertyTree> apTreeList) {
        this.apTreeList = apTreeList;
    }

    public void addApTree(ApplicationPropertyTree... apTrees) {
        if(this.apTreeList==null){
            this.apTreeList=Lists.newArrayList();
        }
        //add时不会出现重复的apTree
        for (ApplicationPropertyTree apTree : apTrees) {
            Stream<ApplicationPropertyTree> applicationPropertyTreeStream = this.apTreeList.stream().filter(u -> Objects.equals(u.getConfigGroupPropertyId(), apTree.getConfigGroupPropertyId()));
            Optional<ApplicationPropertyTree> first = applicationPropertyTreeStream.findFirst();
            if (!first.isPresent()) {
                this.apTreeList.add(apTree);
            }
        }
    }


    public boolean isRootNode(){
        //如果 配置组属性ID为空,则是建立的虚拟顶级节点
        if(getConfigGroupPropertyId()==null||getConfigGroupPropertyId()==0L){
            return true;
        }
        return false;
    }

}
